﻿using Hsp.Auto.Common.Data;
using Hsp.Auto.Interface;
using System;
using System.Configuration;
using System.Linq;
using System.Reflection;

namespace Hsp.Auto.DataFactory
{
    /// <summary>
    /// 简单工厂
    /// </summary>
    public class SimpleFactory
    {
        //private static string helperString = string.Empty;// GetAppSetting("DbHelperRefliction");
        //private static string dllName = helperString.Split(",")[1].Trim(), typeName = helperString.Split(",")[0].Trim();

        //<add key = "MySqlRefliction" value="Hsp.Auto.MySql.MySqlHelper,Hsp.Auto.MySql.dll"/>
        //<add key = "SQLiteRefliction" value="Hsp.Auto.SQLite.SQLiteHelper,Hsp.Auto.SQLite.dll"/>
        //<add key = "OracleRefliction" value="Hsp.Auto.Oracle.OracleHelper,Hsp.Auto.Oracle.dll"/>
        //<add key = "SqlServerRefliction" value="Hsp.Auto.SqlServer.SqlServerHelper,Hsp.Auto.SqlServer.dll"/>

        static SimpleFactory()
        {

        }

        /// <summary>
        /// 简单工厂创建默认实例
        /// </summary>
        /// <returns></returns>
        public static IDbHelper CreateInstance(DBType dbType)
        {
            var reflictionString = GetAppSetting($"{dbType}Refliction");
            var dllName = reflictionString.Split(",")[1].Trim();
            var typeName = reflictionString.Split(",")[0].Trim();

            Assembly assembly = Assembly.LoadFrom(dllName); // dll名称，带文件扩展名            
            Type type = assembly.GetType(typeName); //获取类型            
            Object oDbHelper = Activator.CreateInstance(type);
            IDbHelper dbHelper = oDbHelper as IDbHelper;
            return dbHelper;
        }

        /// <summary>
        /// 简单工厂创建定制实例
        /// </summary>
        /// <param name="name">类名</param>
        /// <returns></returns>
        public static T CreateInstance<T>(DBType dbType, string name) where T : IDbHelper
        {
            var reflictionString = GetAppSetting($"{dbType}Refliction");
            var dllName = reflictionString.Split(",")[1].Trim();
            //var typeName = reflictionString.Split(",")[0].Trim();
            var typeName = dllName.Replace("dll", "") + name;
            Assembly assembly = Assembly.LoadFrom(dllName); // dll名称，带文件扩展名            
            Type type = assembly.GetType(typeName); //获取类型            
            Object oDbHelper = Activator.CreateInstance(type);
            T dbHelper = (T)oDbHelper;
            return dbHelper;
        }

        //public string dd(DBType dbType)
        //{
        //    switch (dbType)
        //    {
        //        //case DBType.SqlServer:
        //        //    return DBType.SqlServer.ToString();// new SqlServerDB(dbType, SqlClientFactory.Instance, connectionString, cmdTimeOut);
        //        //case DBType.MySql:
        //        //    return new MySqlDB(dbType, MySqlClientFactory.Instance, connectionString, cmdTimeOut);
        //        //case DBType.Oracle:
        //        //    return new OracleDB(dbType, OracleClientFactory.Instance, connectionString, cmdTimeOut);
        //        //case DBType.OracleDDTek:
        //        //    return new OracleDDTekDB(dbType, OracleFactory.Instance, connectionString, cmdTimeOut);
        //        //case DBType.PostgreSql:
        //        //    return new PostgreSqlDB(dbType, NpgsqlFactory.Instance, connectionString, cmdTimeOut);
        //        //case DBType.SQLite:
        //        //    return new SQLiteDB(dbType, SQLiteFactory.Instance, connectionString, cmdTimeOut);
        //        //case DBType.DB2:
        //        //    return new DB2DDTekDB(dbType, DB2Factory.Instance, connectionString, cmdTimeOut);
        //        //default:
        //        //    throw new ArgumentException("未支持的数据库类型！");
        //    }
        //}

        /// <summary>
        /// 简单工厂创建默认实例
        /// </summary>
        /// <returns></returns>
        //public static IDbHelper CreateInstance()
        //{
        //    Assembly assembly = Assembly.LoadFrom(dllName); // dll名称，带文件扩展名            
        //    Type type = assembly.GetType(typeName); //获取类型            
        //    Object oDbHelper = Activator.CreateInstance(type);
        //    IDbHelper dbHelper = oDbHelper as IDbHelper;
        //    return dbHelper;
        //}

        /// <summary>
        /// 简单工厂创建定制实例
        /// </summary>
        /// <returns></returns>
        public static T CreateInstance<T>() where T : IDbHelper, new()
        {
            //string name = typeof(T).Name;
            //typeName = dllName.Replace("dll", "") + name;
            //Assembly assembly = Assembly.LoadFrom(dllName); // dll名称，带文件扩展名            
            //Type type = assembly.GetType(typeName); //获取类型            
            //Object oDbHelper = Activator.CreateInstance(type);
            //T dbHelper = (T)oDbHelper;
            //return dbHelper;

            //IDbHelper dbHelper = new SqlServerHelper();
            //return (T)dbHelper;

            //IDbHelper dbHelper = new T();
            return new T();
        }

        /// <summary>
        /// 简单工厂创建定制实例
        /// </summary>
        /// <param name="name">类名</param>
        /// <returns></returns>
        //public static T CreateInstance<T>(string name) where T : IDbHelper
        //{
        //    typeName = dllName.Replace("dll", "") + name;
        //    Assembly assembly = Assembly.LoadFrom(dllName); // dll名称，带文件扩展名            
        //    Type type = assembly.GetType(typeName); //获取类型            
        //    Object oDbHelper = Activator.CreateInstance(type);
        //    T dbHelper = (T)oDbHelper;
        //    return dbHelper;
        //}

        private static Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        /// <summary>
        /// 获取指定节点的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSetting(string key)
        {
            if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
            {
                string value = config.AppSettings.Settings[key].Value;
                return value;
            }
            else
            {
                return null;
            }
        }
    }
}
